<?php

namespace app\admin\controller;


use think\Request;
use think\Db;
class MenuController extends BaseController
{

    public function lst()
    {
        $menus = Db::name('top_menu')->where(['is_show'=>1])->order(['sort'=>'asc'])->select();
        foreach($menus as $k=>$v){
            $menus[$k]['two']=Db::name('top_two_menu')->where(['menu_id'=>$v['menu_id'] ])->order(['menusort'=>'asc'])->select();
        }
        $this->assign([
            'menus'=>$menus
           ]);
        return $this->fetch();
    }
    //添加菜单
    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $menus = Db::name('top_menu')->where(['is_show'=>1])->order(['sort'=>'desc'])->select();
                foreach($menus as $k=>$v){
                    $menus[$k]['two']=Db::name('top_two_menu')->where(['menu_id'=>$v['menu_id'] ])->order(['menusort'=>'desc'])->select();
                }
                 $this->assign([
                        'menus'=>$menus,
                    ]);
                return $this->fetch('/menu/add');
                break;
            case "POST":
                $params = $request->except('/admin/menu/add_html');

                if($params['level'] ==0){
                    $params['level']=1;
                    Db::table('top_menu')->insert($params);
                    $this->success('添加成功',url('admin/menu/lst'),'',1 );
                }
                if( $menu_id=substr($params['level'],0,strpos($params['level'], '_') ) ){
                    
                    if($res=Db::table('top_menu')->where(['menu_id'=>$menu_id])->find() ){
                        $data['level']=2;
                        $data['menu_id']=$res['menu_id'];
                        $data['two_menu']=$params['menu_name'];
                        $data['menusort']=$params['sort'];
                        $data['link']=$params['link'];
                        if(Db::name('top_two_menu')->insert($data) ){
                            $this->success('添加成功',url('admin/menu/lst'),'',1 );
                        }else{
                            $this->success('添加失败',url('admin/menu/lst'),'',1 );
                        }
                    }   
                }
                break;
            default:
                return "错误的数据信息";
        }
    }
    //修改菜单
    public function edi()
    {
        $request = Request::instance();
        $menus = Db::name('top_menu')->where(['is_show'=>1])->order(['sort'=>'asc'])->select();
        switch($request->method() ){
            case "GET":
                 if($request->param('status') ==1 ){
                    $data=Db::name('top_menu')->where(['menu_id'=>$request->param('id')])->find();

                    $data['id']='';
                 }else{
                    $data=Db::name('top_two_menu')->find($request->param('id') );
                 }
                 $this->assign([
                        'menus'=>$menus,
                        'data'=>$data
                    ]);
                 return $this->fetch('menu/edi');
                 break;
            case "POST":
                 $info=$request->except('/admin/menu/edi_html');
                 $level=$request->param('level');  
                if(substr($level,strpos($level, '_')+1 )==1 ){
                      if( !empty($info['menu_id']) ){
                        $this->error('一级菜单不可修改',url("admin/menu/lst"),'',1);
                      }
                       $res= Db::name('top_menu')->where(['menu_id'=>substr($level,0,strpos($level, '_') )])->find();
                       if(!empty($res['menu_name']) && !empty($info['menu_name']) ){
                              $data['level']=2;
                              $data['menu_id']=$res['menu_id'];
                              $data['two_menu']=$info['menu_name'];
                              $data['id']=$info['id'];
                              $data['menusort']=$info['menusort'];
                              $data['is_show']=$info['is_show'];
                              if(Db::table('top_two_menu')->update($data) ){
                                $this->success('更新成功',url("admin/menu/lst"),'',1);
                                exit;
                              }else{
                                $this->error('修改失败',url("admin/menu/lst"),'',1);
                                exit;
                            }
                        }
                        if($res['menu_id'] !=$info['menu_id'] ){
                            $this->error('修改错误',url("admin/menu/lst"),'',1);
                            exit;
                        }
                        if(Db::table('top_menu')->update($info) ){
                            $this->success('更新成功',url("admin/menu/lst"),'',1);
                            exit;
                        }else{
                            $this->error('修改失败',url("admin/menu/lst"),'',1);
                            exit;
                        }
                }
                 break;
            default:
                 return "错误信息";
        }
    }
    //删除菜单
    public function del()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        switch($status){
            case 1:
               if(Db::name('top_two_menu')->where(['menu_id'=>$id])->Count() ){
                     return "请先删除下级菜单!";
                     exit;
               }else{
                    Db::name('top_menu')->delete($id);
                    return "删除成功!";
                    exit;
               }
            case 2:
               if(DB::name('top_two_menu')->delete($id) ){
                    return "删除二级菜单成功!";
                    exit;
               }else{
                    return "删除失败!~";
                    exit;
               }
               break;
            default: 
               return "错误信息";
        }
    }
//    /**
//     * 显示资源列表
//     *
//     * @return \think\Response
//     */
//    base function index()
//    {
//        $nav = Nav::paginate(6);
//        return view('',['list'=>$nav]);
//    }
//
//    /**
//     * 显示创建导航表单页.
//     *
//     * @return \think\Response
//     */
//    base function create()
//    {
//        return view();
//    }
//
//    /**
//     * 保存新建的导航
//     *
//     * @param  \think\Request  $request
//     * @return \think\Response
//     */
//    base function save(Request $request)
//    {
//        if(Nav::create($request->post())){
//            $this->success('添加成功','nav/index','',1);
//        }else{
//            $this->error('添加失败');
//        }
//    }
//
//    /**
//     * 显示编辑导航表单页.
//     *
//     * @param  int  $id
//     * @return \think\Response
//     */
//    base function edit($id)
//    {
//        $nav = Nav::get($id);
//        return view('',['list'=>$nav]);
//    }
//
//    /**
//     * 保存更新的资源
//     *
//     * @param  \think\Request  $request
//     * @param  int  $id
//     * @return \think\Response
//     */
//    base function update(Request $request, $id)
//    {
//        if(Nav::update($request->post(),['id'=>$id])){
//            $this->success('更新成功','nav/index','',1);
//        }else{
//            $this->error('更新失败');
//        }
//    }
//
//    /**
//     * 删除指定资源
//     *
//     * @param  int  $id
//     * @return \think\Response
//     */
//    base function delete($id)
//    {
//        if(Nav::destroy($id)){
//            $this->success('删除成功','nav/index','',1);
//        }else{
//            $this->error('更新失败');
//        }
//    }
}
